﻿/// <reference path="jquery-1.5.1.js" />
/// <reference path="jquery-1.5.1-vsdoc.js" />
function IsNumeric(sText) {
    //return (input - 0) == input && input.length > 0;
    if (sText == undefined || sText == null || sText.length <= 0)
        return false;
    var ValidChars = "0123456789.";
    var IsNumber = true;
    var Char;
    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;
}
function ShowCenteredPopUp(pageURL, windowName, width, height, isScrolling) {
    //alert(pageURL);
    var scrollbars = isScrolling ? 'yes' : 'no';    
    var leftPosition = (screen.width) ? (screen.width - width) / 2 : 0;
    var topPosition = (screen.height) ? (screen.height - height) / 2 : 0;
    topPosition -= 30;
    var settings = 'height=' + height + ',width=' + width + ',top=' + topPosition + ',left=' + leftPosition + ',resizable=no,scrollbars=' + scrollbars + ',menubar=no,toolbar=no,status=yes,location=no,directories=no,addressbar=no'
    var page = pageURL;
    win = window.open(page, windowName, settings);
    if (win != null) {
        win.window.focus();
    }
    return win;
}
function RemoveFromProductCompare(productID, anchor) {
    $(anchor).parent().prepend('<a class="product-compare-link-button" href="javascript:void(0);" onclick="AddToProductCompare(' + productID + ', this)">Compare (+)</a>');
    $(anchor).remove();
    $('#tblProductCompareList input[name=hdnCompareProductID]').each(function () {
        if ($(this).val() == productID) {
            $(this).parent().parent().find('a').each( function(){
                if($(this).html() == 'Remove'){
                    RemoveFromCompareList(productID, $(this)); return;
                }
            });
        }
    });
}
function RemoveFromCompareList(productID, element) {
    Ajax('/Ajax/RemoveProductToCompare', 'productID=' + productID, function (result) {
        if (result == 1) {
            $(element).parent().parent().slideUp('slow', function () { $(this).remove(); RebindCompareAllLink(); ReBindCompareLinks(); });
        }
    });    
}
function AddToProductCompare(productID, element) {
    Ajax('/Ajax/AddProductToCompare', 'productID=' + productID, function (result) {
        if (result.length > 0) {
            var product = eval('(' + result + ')');
            var html = '<tr><td>';
            if (product.ProductImages != null && product.ProductImages.length > 0 && product.ProductImages[0].Thumbnail.length > 0 )
                html += '<img class="product-img" src="/UploadedImages/ProductImages/' + product.ID + '/' + product.ProductImages[0].Thumbnail + '" alt="' + product.Name + ' title="' + product.Name + '" />';
            else
                html += '<img class="product-img" src="/content/images/no_image.gif" alt="' + product.Name + '" title="' + product.Name + '" />';

            html += '<input type="hidden" name="hdnCompareProductID" value="' + product.ID + '" />';
            html += '</td>';
            html += '<td><a href="/product/details/' + product.ID + '/' + product.ProductUrl + '" target="_blank">' + product.Name + '</a></td>';
            html += '<td><a href="javascript:void(0);" onclick="RemoveFromCompareList(' + product.ID + ', this);">Remove</a></td>';
            html += '</tr>';
            $('#tblProductCompareList').append(html);
            RebindCompareAllLink();
            //$(element).parent().prepend('<span class="product-compared-link-button">Remove from Compare</span>');
            $(element).parent().prepend('<a class="product-compared-link-button" href="javascript:void(0);" onclick="RemoveFromProductCompare(' + product.ID + ', this)">Compare (-)</a>');
            $(element).remove(); //.html("Compared").removeClass('product-compare-link-button').addClass('product-compared-link-button');
            ReBindCompareLinks();
        }
    });
}
//Bind Compare List
//var _HasProductInCompareList = false;
$(document).ready(function () {
    var divCompare = $('#divProductCompareList');
    ReBindCompareLinks();
    $('.span-compare').each(function () {
        BindComparePopup(divCompare, $(this), 13, 220);
    });
    $(document).mouseover(function () {
        $(divCompare).hide();
    });
    $(divCompare).mouseover(function (e) {
        e.stopPropagation();
    });
});
function ReBindCompareLinks() {
    var divCompare = $('#divProductCompareList');
    $('a.product-compare-link-button, a.product-compared-link-button').each(function () {
        BindComparePopup(divCompare, $(this), 26, 0);
    });
}
function BindComparePopup(divCompare, element, topAdjustment, leftAdjustment) {
    $(element).hover(function (e) {        
        if ($('#tblProductCompareList tr').length > 0) {
            var position = $(this).position();
            $(divCompare).css('top', position.top + topAdjustment).css('left', (position.left - leftAdjustment)).show();
            e.stopPropagation();
        }
    });
}
function RebindCompareAllLink() {
    var ids = '';    
    $('#tblProductCompareList input[name=hdnCompareProductID]').each(function () {
        if (ids.length == 0)
            ids = $(this).val();
        else
            ids += ',' + $(this).val();
    });
    $('#aCompareAll').attr('href', '/product/compare/' + ids);
    var productCount = ids.length > 0 ? ids.split(',').length : 0; 
    $('.span-compare').each(function () {               
        $(this).html('Compare(' + productCount + ')');
    });
}
function GoComparePage() {
    if($('#tblProductCompareList tr').length > 0)
        window.location = $('#aCompareAll').attr('href');
}
function ShowLoadingAnimation(shoBlockingContainer) {
    $('#divLoadingAnimation').show();
    if (shoBlockingContainer) {
        $('#divBlockingContainer').width($(document).width()).height($(document).height()).show();
        $(window).resize(function () {
            $('#divBlockingContainer').width($(document).width()).height($(document).height());
        });
    }
}
function HideLoadingAnimation() {
    $('#divLoadingAnimation').hide();
    $('#divBlockingContainer').hide();
}

function Ajax(url, data, successCallback) {
    $.ajax({
        type: 'POST',
        url: url,
        data: data,
        success: function (result) {
            successCallback(result);
        },
        statusCode: {
            404: function () {
                alert('ERROR: Unable to complete the AJAX Request.\nRequested URL: ' + url + ' was not found.\nPlease contact with site administrator.');
            }           
        }
    });
}
function AjaxGET(url, successCallback) {
    $.ajax({
        type: 'GET',
        url: url,
        success: function (result) {
            successCallback(result);
        },
        statusCode: {
            404: function () {
                alert('ERROR: Unable to complete the AJAX Request.\nRequested URL: ' + url + ' was not found.\nPlease contact with site administrator.');
            }
        }
    });
}